﻿#include "homepage.h"
#include "ui_homepage.h"

HomePage::HomePage(OpcUA_QThread * opcUA, QWidget *parent) :
    QWidget(parent),
    myOpcUA(opcUA),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //以下是加入主窗口
    myGraphicsWidget = new GraphicsWidget(this);
    myGraphicsWidget->initOpcUA(opcUA);
    myGraphicsWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QLayout * window = new QVBoxLayout;
    window->setContentsMargins(0, 0, 0, 0);
    window->addWidget(myGraphicsWidget);

    ui->widget->setLayout(window);

    //一秒读取一次PLC数据的定时器。
    myReadDataTimer = new QTimer(this);
    myReadDataTimer->setInterval(1000);
    connect(myReadDataTimer, &QTimer::timeout, this, &HomePage::on_readData);

    //当OPCUA读取完数据，本函数获取数据
    connect(myOpcUA, &OpcUA_QThread::signals_sendDataToHomePage, this, &HomePage::on_receiveData);

    //当SQLite数据库创建成功，发送signals_dbCreateFinished信号，设置状态值。
    mySQLite = new MySQLite(this);
    connect(mySQLite, &MySQLite::signals_dbCreateFinished, this, &HomePage::on_dbCreateFinished);

    myReadDataTimer->start();
}

HomePage::~HomePage()
{
    if(myReadDataTimer->isActive())
        myReadDataTimer->stop();
    delete myReadDataTimer;
    delete ui;
}

void HomePage::on_FlowChart_Start()
{
    //FlowChart 开始生长流程了，这里应该修改db数据库的写入数据，设置对应的标志
}

void HomePage::on_FlowChart_Stop()
{
    //FlowChart 停止生长流程了，这里应该修改db数据库的写入数据，复位对应的标志
}

void HomePage::on_receiveDesiredValue(QVector<int16_t> flowmeter, QVector<int16_t> pressuremeter)
{
    //这里的数据是从FlowChart界面读取的，不需要转化
    double myFlowmeter[11];
    myFlowmeter[0] = flowmeter[0];
    myFlowmeter[1] = flowmeter[1];
    myFlowmeter[2] = flowmeter[2];
    myFlowmeter[3] = flowmeter[3];
    myFlowmeter[4] = flowmeter[4];
    myFlowmeter[5] = flowmeter[5];
    myFlowmeter[6] = flowmeter[6];
    myFlowmeter[7] = flowmeter[7];
    myFlowmeter[8] = flowmeter[8];

    double myPressuremeter[2];
    myPressuremeter[0] = pressuremeter[0];
    myPressuremeter[1] = pressuremeter[1];

    myGraphicsWidget->updateAllDesiredValue(myFlowmeter, myPressuremeter);
}

void HomePage::on_readData()
{
    myOpcUA->readDataToHomePage();
}

void HomePage::on_receiveData(QVector<int16_t> flowmeter, QVector<int16_t> pressuremeter, QVector<bool> valve, QVector<int16_t> temperature, int32_t dintPressure, bool success)
{
    //qDebug() << u8"homePage : receiveData!" << u8"读取是否成功:" << success << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    //这里的数据是从kepserver读取的，需要转化后才能使用
    double myFlowmeter[11];
    myFlowmeter[0] = PLCDataConversion::convert_24576to3000(flowmeter[0]);
    myFlowmeter[1] = PLCDataConversion::convert_24576to1000(flowmeter[1]);
    myFlowmeter[2] = PLCDataConversion::convert_24576to300(flowmeter[2]);
    myFlowmeter[3] = PLCDataConversion::convert_24576to1000(flowmeter[3]);
    myFlowmeter[4] = PLCDataConversion::convert_24576to30(flowmeter[4]);
    myFlowmeter[5] = PLCDataConversion::convert_24576to1000(flowmeter[5]);
    myFlowmeter[6] = PLCDataConversion::convert_24576to100(flowmeter[6]);
    myFlowmeter[7] = PLCDataConversion::convert_24576to300(flowmeter[7]);
    myFlowmeter[8] = PLCDataConversion::convert_24576to1000(flowmeter[8]);

    double myPressuremeter[2];
    myPressuremeter[0] = PLCDataConversion::convert_24576to300(pressuremeter[0]);
    myPressuremeter[1] = PLCDataConversion::convert_24576to300(pressuremeter[1]);

    bool myValve[11];
    for(int i = 0; i < 11; i++)
    {
        myValve[i] = valve.at(i);
    }

    myGraphicsWidget->updateAllActualValue(myFlowmeter, myPressuremeter, myValve);

    //温度显示  临时
    ui->lineEdit_21->setText( QString::number(temperature[0]) );
    ui->lineEdit_22->setText( QString::number(temperature[1]) );
    //压力显示  临时
    ui->lineEdit_23->setText( QString::number(dintPressure));

    emit signals_sendTemperature(temperature[0], temperature[1]);
}

void HomePage::on_writeDB(const QVector<int16_t>& result1, QVector<bool>& result2, bool success)
{
    if( success )
    {
        SensorData myData;

        //赋值
        myData.pressure =  result1[1] ;

        //
        //
        //
        //

        //保存到数据库
        mySQLite->writeSingleData(myData);
    }
}


void HomePage::on_dbCreateFinished(bool success, QString dbName)
{
    if( success )
        myReadyStatus = true;
    else
    {
        myReadyStatus = false;
        qDebug() << u8"警告：数据库" << dbName << u8"创建失败";
    }
}

